import java.util.HashSet;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * Created by loso on 2017/3/26.
 */
public class _414_ThirdMaximumNumber {

    int[] tmax = new int[]{Integer.MIN_VALUE,Integer.MIN_VALUE,Integer.MIN_VALUE};

    public int thirdMax(int[] nums) {
        HashSet<Integer> set = new HashSet<>();
        for (int i = 0; i < nums.length; i++) {
            set.add(nums[i]);
        }
        if (set.size()==1){
            for (int i :
                    set) {
                return i;
            }
        }else if (set.size() ==2){
            int max = Integer.MIN_VALUE;
            for (int i :
                    set) {
                max = i>max?i:max;
            }
            return max;
        }

        for (int i :
                set) {
            check(i);
        }
        return tmax[2];
    }

    void check(int n){
        if (n==tmax[0]||n==tmax[1]||n==tmax[2]){
            return;
        }
        if (n>tmax[0]){
            tmax[2] = tmax[1];
            tmax[1] = tmax[0];
            tmax[0] = n;
        }
        else if(n>tmax[1]){
            tmax[2] = tmax [1];
            tmax[1] = n;
        }
        else if (n>tmax[2]){
            tmax[2] = n;
        }
    }

}
